<?php
class craw extends CI_Controller{
	
	protected $simple_html_dom;
	
	function __construct()
	{
		parent::__construct();
		//$this->simple_html_dom = $this->load->library("simple_html_dom");
		//$this->load->library(array("simple_html_dom"));
		include_once APPPATH."libraries/simple_html_dom.php";
	}
	
	function phat_am($link)
	{
		//$link = "http://www.tienganh123.com/phat-am-tieng-anh-co-ban/1482-long-vowel-i-nguyen-am-dai-i.html";
		$html = file_get_html($link);
		$div = $html->find("div[class='comment_bai_hoc']",0);
		$title = $html->find("div.vd_box_tit_te",0)->plaintext;
		$content = $div->outertext;
		//$content = $html->find("td.containt_center_cen_left",0)->outertext;
		$array = array("title" => $title, "content" => $content, "link" => $link, "time" => date("Y-m-d H:i:s"));
		$this->db->set($array);
		$this->db->insert('phat_am');
	}
	
	function get_link2()
	{
		
		$url = "http://www.tienganh123.com/phat-am-tieng-anh-co-ban";
		$html = file_get_html($url);
		$pag = $html->find("div.lng_pagi",0)->find("a");
		$arr = array();
		//$arr[] = $url;
		foreach($pag as $k => $v){
			//$arr[] = $this->get_link_page($v->href);
			array_push($arr,$this->get_link_page($v->href));
		}
		
		$tmp = array();
		foreach($arr as $k => $v){
			foreach($v as $vv){
				$tmp[] = $vv;
			}
		}
		
		
		foreach($tmp as $k => $v){
			$this->phat_am($v);
		}
		
	}
	
	function get_link_page($url)
	{
		$tmp = array();
		$html = file_get_html($url);
		$ul = $html->find("ul[class='ptl_content']",0);
		foreach($ul->find("li") as $k => $li){
			$href = $li->find("a",0)->href;
			$tmp[] = $href;
		}
		return $tmp;
	}
	
	function get_word()
	{
		//$link = "http://www.tienganh123.com/6000-tu-tieng-anh-thong-dung/7041-bai-2.html";
		$link = "http://www.tienganh123.com/1000-tu-vung-tieng-anh-co-ban/1405-bai-2.html";
		
		$type = explode('/', $link);
		$arr = array();
		if($type[3] == '1000-tu-vung-tieng-anh-co-ban')
		{
			$arr = $this->get_1000_word($link);
		}
		
		print_r($arr);die;
		
		$html = file_get_html($link);
		
		// Find all images
		$table = $html->find('table[width=500]',0);
		//echo $table;
		
		$trs = $table->find('tr');
		unset($table);
		
		foreach($trs as $k => $v)
		{
			if($k > 0)
			{
				$tmp = array();
				$tds = $v->find('td');
				foreach($tds as $k1 => $td){
					if($k1 > 0){
						if($k1 != 4)
						{
							$td = $td->text();
							$tmp[] = $td;
						}
						else
						{
							$td = $td->find("span span",0);
							//$td = str_replace('media-url', 'media_url', $td);
							$td = $td->attr;
							$td = $td["media-url"];
							//preg_match('/.*?media-url=*[\s\'\"](.*?)*[\s\'\"].*?/is', $td, $preg);
							//print_r( $td);die;
							//print_r($preg);die;
							//$td = $td->innertext();
							$tmp[] = $td;
						}
						
					}
				}
				unset($tds);
				$arr[] = $tmp;
			}
		}
		unset($trs);
		print_r($arr);
		$html->clear();
		unset($html);
	}
	
	function get_1000_word($link)
	{
		$arr = array();
		$html = file_get_html($link);
		$table = $html->find('table[width=650]',0);
		
		$trs = $table->find('tr');
		unset($table);
		
		foreach($trs as $k => $v)
		{
			if($k > 0)
			{
				$tmp = array();
				$tds = $v->find('td');
				foreach($tds as $k1 => $td){
					//if($k1 > 0){
						if($k1 != 3)
						{
							$td = $td->text();
							$tmp[] = $td;
						}
						else
						{
							
							$td = $td->find("span span",0);
							//$td = str_replace('media-url', 'media_url', $td);
							$td = $td->attr;
							$td = $td["media-url"];
							//preg_match('/.*?media-url=*[\s\'\"](.*?)*[\s\'\"].*?/is', $td, $preg);
							//print_r( $td);die;
							//print_r($preg);die;
							//$td = $td->innertext();
							$tmp[] = $td;
						}
		
					//}
				}
				unset($tds);
				$arr[] = $tmp;
			}
		}
		unset($trs);
		$html->clear();
		unset($html);
		return $arr;
	}
	
	function get_link()
	{
		$str = "10 1 1 10 9 10 9 1 10 8 9 9 9 9 7 8 7 10 9 8 10 10 9 5 10 1 9 10 9 8 9 9 10 7 10 10 10 7 10 1 10 6 9 10 1 1 1 1 1 1 9 9 8 9 10 7 8 9 10 3 1 10 2 2 1 4 5 1 10 10 1 1 2 1 1 4 1 1 1 7 9 5 5 6 10 9 10 9 10 10 10 10 1 1 1 10 10 10 10 10 10 9 10 10 10 1 10 10 10 10 10 9 10 1 9 10 10 10 10";
		$tmp = explode(' ', $str);
		$sum = 0;
		$i = 0;
		foreach($tmp as $k => $v)
		{
			$sum += $v;
			$i++;
		}
		echo "$sum/$i=".round($sum/$i,1);
	}
}